package com.example.himalayaproject.find.broadcast.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.himalayaproject.R;
import com.example.himalayaproject.find.broadcast.bean.grid.GridItem;
import com.example.himalayaproject.find.broadcast.callback.OnPageLoadListener;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by Administrator on 2016/12/13.
 */

public class GridItemAdapter extends RecyclerView.Adapter<GridItemAdapter.ViewHolder>{
    private Context context;
    List<GridItem> gridItems;
    private OnPageLoadListener listener;

    public GridItemAdapter(Context context,OnPageLoadListener listener) {
        this.context = context;
        this.listener = listener;
        gridItems = new ArrayList<>();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder holder = new ViewHolder(LayoutInflater.from(context).inflate(R.layout.broadcast_item, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (position != gridItems.size()-1) {
            GridItem gridItem = gridItems.get(position);
            holder.nameTextView.setText(gridItem.getName());
            if (gridItem.getProgramName() == null){
                holder.currTextView.setText("暂无节目单");
            } else {
                holder.currTextView.setText("正在直播："+gridItem.getProgramName());
            }
            float count = ((float) Integer.parseInt(gridItem.getPlayCount()))/10000;
            holder.numberTextView.setText(count+"万人");
            Picasso.with(context).load(gridItem.getCoverSmall()).into(holder.srcImage);
        } else {
            listener.onPageLoad();
        }
    }

    public void addAll(Collection<? extends GridItem> collection){
        int size = gridItems.size();
        gridItems.addAll(collection);
        notifyItemRangeInserted(size,collection.size());
    }

    @Override
    public int getItemCount() {
        return gridItems.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView nameTextView,currTextView,numberTextView;
        private ImageView imageView,srcImage;
        public ViewHolder(View itemView) {
            super(itemView);
            nameTextView = ((TextView) itemView.findViewById(R.id.bct_item_name));
            currTextView = ((TextView) itemView.findViewById(R.id.bct_item_current));
            numberTextView = ((TextView) itemView.findViewById(R.id.bct_item_number));
            imageView = ((ImageView) itemView.findViewById(R.id.bct_item_play));
            srcImage = ((ImageView) itemView.findViewById(R.id.bct_item_src));
        }
    }
}
